package com.example.home.adapter.provider

import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.entity.BaseEntity
import com.example.home.R
import com.example.home.adapter.CategoryItemAdapter
import com.example.home.adapter.DiscoverItemType
import com.example.home.databinding.ItemHomeCategoryCardViewBinding
import com.example.home.entity.discover.CardDataItemData
import com.example.home.entity.discover.CategoryCardEntity

class CategoryCardProvider : BaseItemProvider<BaseEntity>() {

    private lateinit var itemCategoryItemAdapter: CategoryItemAdapter

    override val itemViewType: Int = DiscoverItemType.CATEGORY_CARD_VIEW
    override val layoutId: Int= R.layout.item_home_category_card_view

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
       var dataBinding= DataBindingUtil.bind<ItemHomeCategoryCardViewBinding>(viewHolder.itemView)
        dataBinding?.rvCategoryView?.layoutManager=GridLayoutManager(viewHolder.itemView.context,2,RecyclerView.HORIZONTAL,false)


    }
    override fun convert(helper: BaseViewHolder, item: BaseEntity) {

        var dataBinding=helper.getBinding<ItemHomeCategoryCardViewBinding>()
        if(item!=null && dataBinding!=null){
            var entity = item as CategoryCardEntity
            dataBinding.tvCategoryTitle.text=entity.data.header.title
            dataBinding.tvActionTitle.text=entity.data.header.rightText

            //设置分类列表

            var dataList = mutableListOf<CardDataItemData>()

            entity.data.itemList.forEach {
                dataList.add(it.data)
            }


            itemCategoryItemAdapter= CategoryItemAdapter(R.layout.item_home_category_item_card_view)
            dataBinding.rvCategoryView.adapter = itemCategoryItemAdapter
            itemCategoryItemAdapter.setNewInstance(dataList)


        }
    }
}